package com.bonc.ioc.controller;

import com.bonc.ioc.base.controller.BaseController;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.service.TopicService;
import com.bonc.ioc.util.CurrentUser;
import com.bonc.ioc.validation.AppAuthAnnotation;
import com.bonc.ioc.vo.TopicVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0
 * @description:
 * @author: wangqinglin
 * @date: 2021/3/18 16:00
 * @change : 2021/3/18 16:00 by q8541q@163.com  for init
 */

@RestController
@RequestMapping("/topic")
@Slf4j
public class TopicController extends BaseController {

    @Autowired
    private TopicService topicService;

    @Autowired
    private CurrentUser currentUser;

    /**
     * saveAddictsInfo
     * 保存专题信息
     *
     * @param item:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/18 15:41
     * @change 2021/3/18 15:41 by q8541q@163.com for init
     * @since 1.0.0
     */
    @PostMapping("/save")
    @AppAuthAnnotation
    public AppReply saveAddictsInfo(@RequestBody TopicVo item) {
        if (item.getId() != null) {
            item.setModifyUser(currentUser.getUserId(request));
        } else {
            item.setModifyUser(currentUser.getUserId(request));
            item.setCreateUser(currentUser.getUserId(request));
        }
        return AppReply.success(topicService.save(item));
    }

    /**
     * deleteMadInfo
     * 删除专题信息
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/18 15:41
     * @change 2021/3/18 15:41 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/delete")
    @AppAuthAnnotation
    public AppReply deleteMadInfo(@RequestParam String id) {
        return AppReply.success(topicService.deleteByPrimaryKey(id));
    }

    /**
     * getStandardInfo
     * 获取专题详情
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/18 15:44
     * @change 2021/3/18 15:44 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/getTopicInfo")
    public AppReply getStandardInfo(@RequestParam String id) {
        return AppReply.success(topicService.selectByPrimaryKey(Long.valueOf(id)));
    }

    @GetMapping("/getTopicByPage")
    public AppReply getStandardInfoByPage() throws BindException {
        PageResult pageResult = topicService.selectTopicInfoByPage(getPageData());
        return AppReply.success(pageResult);
    }
}
